package com.ctgu.server.utils.clock;

//import com.ctgu.server.dao.entity.ClockLocation;

import com.ctgu.server.dao.entity.AttendanceLocation;

/**
 * @projectName: ctguqmx_server
 * @package: com.ctgu.server.utils
 * @className: LoacationUtils
 * @author: Kvzjj
 * @description: TODO
 * @date: 2023/6/10 21:57
 * @version: 1.0
 */
public class LocationUtils {
    private static final double jl_jd = 102834.74258026089786013677476285;// 每经度单位米;
    private static final double jl_wd = 111712.69150641055729984301412873;// 每纬度单位米;
//    long raidus = 10000; //半径10km
//    double lon = 116.510958; //当前经度
//    double lat = 39.90786; //当前纬度
//    double lon1=116.510842;
//    double lat1=39.90777;
//        double lon2=108.9644583556;
//        double lat2=34.286439088548;
    /**
     * 默认地球半径
     */
    private static double EARTH_RADIUS = 6371000;//赤道半径(单位m)

    /**
     * 转化为弧度(rad)
     * */
    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }
    /**
     * @param lon1 第一点的精度
     * @param lat1 第一点的纬度
     * @param lon2 第二点的精度
     * @param lat2 第二点的纬度
     * @return 返回的距离，单位m
     * */
    public static double GetDistance(double lon1,double lat1,double lon2, double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    /**
     * 计算中心经纬度与目标经纬度的距离（米）
     *
     * @param centerLon
     *            中心精度
     * @param centerLat
     *            中心纬度
     * @param targetLon
     *            需要计算的精度
     * @param targetLat
     *            需要计算的纬度
     * @return 米
     */
    private static double distance(double centerLon, double centerLat, double targetLon, double targetLat) {


        double b = Math.abs((centerLat - targetLat) * jl_jd);
        double a = Math.abs((centerLon - targetLon) * jl_wd);
        return Math.sqrt((a * a + b * b));
    }
    /**
     * @param targetLon:
     *                 目标经度
     * @param targetLat:
     *                 目标维度
     * @param location:
     *                 打卡参数
     * @return boolean
     * @author kvzjj
     * @description TODO 计算两个坐标是否小于目标长度
     * @date 2023/6/12 15:59
     */
    public static boolean distance(double targetLon, double targetLat, LocationEntity location) {
      return distance(location.getLongitude(),location.getLatitude(),targetLon,targetLat)<location.getLength();
    }
    public static boolean distance(double targetLon, double targetLat, AttendanceLocation location) {
        return distance(location.getLongitude(),location.getLatitude(),targetLon,targetLat)<location.getLocationRange();
    }


//    public static void main(String []args){
//
//
//        double dist;
//        double dist2;
//        dist=GeoUtil.GetDistance(lon, lat, lon1, lat1);
//        dist2=GeoUtil.distance(lon, lat, lon1, lat1);
//        System.out.println("1.两点相距：" + dist + " 米");
//        System.out.println("2.两点相距：" + dist2 + " 米");
//    }
}
